{% extends "admin/layout.html" %}

{% block content %}
<div class="container-fluid">
    <!-- 添加新配置表单 -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">添加数据库配置</h6>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('admin.admin_config.database_config') }}">
                        {{ form.hidden_tag() }}
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.db_type.label(class="form-label") }}
                                    {{ form.db_type(class="form-select") }}
                                    {% for error in form.db_type.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.host.label(class="form-label") }}
                                    {{ form.host(class="form-control", placeholder="例如: localhost") }}
                                    {% for error in form.host.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.port.label(class="form-label") }}
                                    {{ form.port(class="form-control", placeholder="例如: 3306") }}
                                    {% for error in form.port.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.database.label(class="form-label") }}
                                    {{ form.database(class="form-control", placeholder="数据库名称") }}
                                    {% for error in form.database.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.username.label(class="form-label") }}
                                    {{ form.username(class="form-control", placeholder="数据库用户名") }}
                                    {% for error in form.username.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.password.label(class="form-label") }}
                                    {{ form.password(class="form-control", placeholder="数据库密码", type="password") }}
                                    {% for error in form.password.errors %}
                                        <span class="text-danger">{{ error }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-check">
                                    {{ form.is_active(class="form-check-input") }}
                                    {{ form.is_active.label(class="form-check-label") }}
                                </div>
                            </div>
                        </div>

                        <div class="row mt-3">
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save me-1"></i> 保存配置
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 现有配置列表 -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">数据库配置列表</h6>
                    <a href="{{ url_for('admin.admin_backup.list_backups') }}" class="btn btn-sm btn-info">
                        <i class="fas fa-database me-1"></i> 查看备份列表
                    </a>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                            <thead>
                                <tr>
                                    <th>数据库类型</th>
                                    <th>主机</th>
                                    <th>端口</th>
                                    <th>数据库名</th>
                                    <th>用户名</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for config in configs %}
                                <tr>
                                    <td>{{ config.db_type }}</td>
                                    <td>{{ config.host }}</td>
                                    <td>{{ config.port }}</td>
                                    <td>{{ config.database }}</td>
                                    <td>{{ config.username }}</td>
                                    <td>
                                        {% if config.is_active %}
                                            <span class="badge bg-success">激活</span>
                                        {% else %}
                                            <span class="badge bg-secondary">未激活</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <a href="{{ url_for('admin.admin_config.delete_database_config', id=config.id) }}"
                                           class="btn btn-sm btn-danger"
                                           onclick="return confirm('确定要删除此配置吗？')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 备份操作 -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">数据库备份操作</h6>
                </div>
                <div class="card-body">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        当前激活的配置:
                        {% if active_config %}
                            {{ active_config.db_type }} - {{ active_config.host }}:{{ active_config.port }}/{{ active_config.database }}
                        {% else %}
                            无激活配置
                        {% endif %}
                    </div>

                    <form method="POST" action="{{ url_for('admin.admin_backup.perform_backup') }}">
                        <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-primary"
                                {% if not active_config %}disabled{% endif %}>
                            <i class="fas fa-database me-1"></i> 立即备份数据库
                        </button>

                        {% if not active_config %}
                        <div class="text-danger mt-2">
                            <i class="fas fa-exclamation-triangle me-1"></i>
                            请先添加并激活一个数据库配置
                        </div>
                        {% endif %}
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}